/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
 */

import org.opengauss.driver.DriverInfoManager;
import org.opengauss.driver.IDriverInfo;
import common.testcase.BaseJdbcTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.sql.rowset.serial.SerialClob;
import java.io.IOException;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * description:this for BlobTest Class
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021.
 *
 * @author Administrator
 * @version [openGauss_debug 0.0.1 2021/3/11]
 * @since 2021/3/11
 */
public class ClobTest extends BaseJdbcTestCase {
    private static final String tableName = "t_clob_test";
    IDriverInfo info = DriverInfoManager.getInfo();
    Connection conn = null;
    @Before
    public void setUp() throws SQLException {
        conn = DriverInfoManager.getConnection(info);
        dropTable();
        createTable();
        initAutoId();
    }
    
    @After
    public void teardown() throws SQLException {
        dropTable();
        conn.close();
        conn = null;
    }
    
    @Test
    public void test() throws SQLException, IOException {
        String sql = "insert into " + getTableName() + "(id, data) values " + " (?, ?)";
        Clob data = new SerialClob("abcd".toCharArray());
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, 0);
            ps.setClob(2, data);
            ps.execute();
        }
        
        String query = "select id, data from " + getTableName() + " where id = 0";
        try (Statement st = conn.createStatement()) {
            try (ResultSet rs = st.executeQuery(query)) {
                while (rs.next()) {
                    System.out.println("get data");
                    int id = rs.getInt(1);
                    Clob data1 = rs.getClob(2);
                    System.out.println(data1);
    
//                    PGClob pgClob = (PGClob) data1;
//                    pgClob.setClob(data1);
//                    Writer writer = pgClob.setCharacterStream(1);
//                    writer.write("ddd");
                    
                }
            }
        }
    }
    
    @Test
    public void testInsertNull() throws SQLException, IOException {
        String sql = "insert into " + getTableName() + "(id, data) values " + " (?, ?)";
        Clob data = null;
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, 0);
            ps.setClob(2, data);
            ps.execute();
        }
        
        String query = "select id, data from " + getTableName() + " where id = 0";
        try (Statement st = conn.createStatement()) {
            try (ResultSet rs = st.executeQuery(query)) {
                while (rs.next()) {
                    System.out.println("get data");
                    int id = rs.getInt(1);
                    Clob data1 = rs.getClob(2);
                    System.out.println(data1);
                }
            }
        }
    }
    
    @Test
    public void testSetObjectNull() throws SQLException, IOException {
        String sql = "insert into " + getTableName() + "(id, data) values " + " (?, ?)";
        Clob data = null;
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, 0);
            ps.setObject(2, null, Types.CLOB);
            ps.execute();
        }
        
        String query = "select id, data from " + getTableName() + " where id = 0";
        try (Statement st = conn.createStatement()) {
            try (ResultSet rs = st.executeQuery(query)) {
                while (rs.next()) {
                    System.out.println("get data");
                    int id = rs.getInt(1);
                    Clob data1 = rs.getClob(2);
                    System.out.println(data1);
                }
            }
        }
    }
    
    @Test
    public void testInsertNullBatch() throws SQLException, IOException {
        String sql = "insert into " + getTableName() + "(id, data) values " + " (?, ?)";
        Object[] inputs = {null, ""};
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, 0);
            ps.setObject(2, null, Types.CLOB);
            ps.addBatch();
            ps.setInt(1, 1);
            ps.setObject(2, "");
            ps.addBatch();
            ps.executeBatch();
        }
        
        String query = "select id, data from " + getTableName() + " where id = 0";
        try (Statement st = conn.createStatement()) {
            try (ResultSet rs = st.executeQuery(query)) {
                while (rs.next()) {
                    System.out.println("get data");
                    int id = rs.getInt(1);
                    Clob data1 = rs.getClob(2);
                    System.out.println(data1);
                }
            }
        }
    }
    
    @Override
    public Connection getConn() {
        return conn;
    }
    
    @Override
    public String getTableName() {
        return tableName;
    }
    
    @Override
    public String getTableColumns4Create() {
        return "(id int primary key, data clob)";
    }
}
